$(document).ready(function() {
    init_menu();
    init_header_sprite();
//    init_signin();
    init_partners_list();
    init_scrolling_photos();
    init_defaultValues();
    init_lightbox();
//    $("form").validate();

    $("img").lazyload();
});

function init_menu() {

 $("#navigation ul.menu li.menu_level_0")
  .data( "timer", null )
  .mouseenter( function()
   {
       $.each( $("#navigation ul.menu li.menu_level_0 ul"), function(){
           $(this).hide();
       });
       window.clearTimeout( $(this).data( "timer" ) );
       $(this).children( "ul" ).show();
   } )
  .mouseover( function()
   {
        window.clearTimeout( $(this).data( "timer" ) );
   } )
   .mouseout( function()
   {
        var li = this;
        $(this).data( "timer", window.setTimeout( function()
                                                  {
                                                   $(li).children( "ul" ).hide();
                                                  }, 300 ) );
  } );
}

function init_header_sprite() {
   $('.header-sprite .social-network').hover( function()
   {
       $(this).animate({
            'margin-top': '-18px'
       }, 200, 'linear');
   },
   function()
   {
       $(this).animate({
            'margin-top': '0px'
       }, 200, 'linear');
   });
}

//function init_signin(){
//
//   $('#signin_form').mouseleave(function(){
//    if($('#signin_form input:focus').length){
//        $('#signin_form').addClass('isFocused');
//    }else{
//        $('#signin_form').removeClass('isFocused');
//    }
//   });
//}

function init_partners_list(){
   var widthPartnersList = 0;
   $.each( $('#scrolling-partners ul li'), function(){
    widthPartnersList += $(this).width() + 20;
   });

   $('#scrolling-partners .rolling-partners').append($('#scrolling-partners ul').clone());
   $('#scrolling-partners ul').css('width', (widthPartnersList) + 'px' );
   $('#scrolling-partners ul:eq(1)').css('left', widthPartnersList + 'px' );
   rotate_partners(widthPartnersList);
}

function rotate_partners(widthPartnersList)
{
 $('#scrolling-partners .rolling-partners').animate({
  left: '-='+ widthPartnersList
 }, 20000, 'linear', function() {
  //$('#scrolling-partners .rolling-partners').append( $('#scrolling-partners ul:eq(0)') );
  $('#scrolling-partners .rolling-partners').css('left', '0px');
  rotate_partners(widthPartnersList);
 });
}

function init_scrolling_photos(){
    window.setInterval( function(){ rotate_photos( $("ul#scrolling-photos"), "li.photo" ); }, 5000 );
}

function rotate_photos( element, filter ){
    if( $( filter+ ":visible", element ).nextAll( filter + ":hidden:eq(0)" ).length == 1 ) {
        $( filter + ":visible", element ).fadeOut( "slow" );
        $( filter+ ":visible", element ).nextAll( filter + ":hidden:eq(0)" ).fadeIn( "slow" );
    }else if( $( filter+ ":eq(0):hidden", element ).length == 1 ){
        $( filter + ":visible", element ).fadeOut( "slow" );
        $( filter+ ":eq(0):hidden", element ).fadeIn( "slow" );
    }
}

function init_defaultValues() {

 $("input[alt], textarea[alt]").bind( 'focus', function()
 {
  if( $(this).data( "isEmpty" ) )
  {
   $(this).data( "isEmpty", false );
   $(this).removeClass( "defaultValue" );
   $(this).val( "" );
  }
 } );

$("input[alt], textarea[alt]").bind( 'blur', function()
{
 if( $(this).val() == "" )
 {
  $(this).data( "isEmpty", true );
  $(this).val( $(this).attr( "alt" ) );
  $(this).addClass( "defaultValue" );
 }
 else
 {
  $(this).data( "isEmpty", false );
  $(this).removeClass( "defaultValue" );
 }
} );

$("input[alt], textarea[alt]").each( function()
{
 if( $(this).val() == "" || $(this).val() == $(this).attr( "alt" ) )
 {
  $(this).data( "isEmpty", true );
  $(this).val( $(this).attr( "alt" ) );
  $(this).addClass( "defaultValue" );
 }
 else
  $(this).data( "isEmpty", false );
 } );
}

function clearDefaultValues( form ){
     $.each( $(form).find(':input'), function(){
         if( $(this).val() == "" || $(this).val() == $(this).attr( "alt" ) ){
            $(this).val("");
         }
     });
}

function init_lightbox(){
    if($('a.lightbox').lightBox){
        $('a.lightbox').lightBox({
            overlayBgColor: '#000',
            overlayOpacity: 0.6,
            containerResizeSpeed: 350
        });
    }
}
/*
function initTinyMce(){
    tinyMCE.init({
        mode:                              "textareas",
        theme:                             "advanced",
        width:                             "473px",
        height:                            "114px",
        theme_advanced_toolbar_location:   "top",
        theme_advanced_toolbar_align:      "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing:           true,
        theme : "advanced",
        mode : "textareas",
        language : "fr",
        relative_urls : false,
        cleanup : true,
        plugins : "paste,table,advimage,advlink,emotions,preview,searchreplace,print,contextmenu,fullscreen",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,zoom,forecolor,backcolor,|,link,unlink,image,|,emotions,fullscreen",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_disable : "help",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_resizing : true,
        theme_advanced_resize_horizontal : false,
        theme_advanced_resizing_use_cookie : false,
        indentation : "10px",
        skin : "o2k7",
        skin_variant : "black",
        width : "100%",
        height : "100"
  });
}*/
/*
function initTinyMce() {
 tinyMCE.init({
   mode : "textareas",
   theme : "advanced",
   language : "fr",
   relative_urls : false,
   cleanup : true,
   plugins : "paste,table,advimage,advlink,emotions,preview,searchreplace,print,contextmenu,fullscreen",
//   theme_advanced_buttons1_add : "fontselect,fontsizeselect",
//   theme_advanced_buttons2_add : "zoom,forecolor,backcolor,separator,emotions,fullscreen",
   theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,zoom,forecolor,backcolor,|,link,unlink,image,|,emotions,fullscreen",
   theme_advanced_buttons2 : "",
   theme_advanced_buttons3 : "",
   theme_advanced_disable : "help",
   theme_advanced_toolbar_location : "top",
   theme_advanced_toolbar_align : "left",
   // theme_advanced_statusbar_location : "bottom",
   // plugin_insertdate_dateFormat : "%d/%m/%Y",
   // plugin_insertdate_timeFormat : "%H:%M:%S",
   theme_advanced_resizing : true,
   theme_advanced_resize_horizontal : false,
   theme_advanced_resizing_use_cookie : false,
   indentation : '10px',
   skin : "o2k7",
   skin_variant : "black",
   width : '100%',
   height : '100'
  });
}*/